package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.mapper.AgoniProductMapper;
import com.ruoyi.system.mapper.AgoniVirtualStoreMapper;
import com.ruoyi.system.pojo.dto.ListOtherProductsByPageDTO;
import com.ruoyi.system.pojo.entity.Product;
import com.ruoyi.system.pojo.vo.MyProductVO;
import com.ruoyi.system.pojo.vo.OtherProductVO;
import com.ruoyi.system.service.IAgoniProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class IAgoniProductServiceImpl implements IAgoniProductService {

    @Autowired
    private AgoniProductMapper agoniProductMapper;

    @Autowired
    private AgoniVirtualStoreMapper agoniVirtualStoreMapper;
    @Override
    public List<Product> listProducts() {
        return agoniProductMapper.listProducts();
    }

    @Override
    public List<Product> listServiceProducts(Long storeId) {
        return agoniProductMapper.findProductsByStoreId(storeId);
    }

    //添加产品

    @Override
    public int addProduct(Product product) {
        return agoniProductMapper.addProduct(product);
    }

    /**
     * web更新产品信息
     */
    @Override
    public int updateProduct(Long id, Product product) {
        //打印日志
        System.out.println("更新产品信息");
        //打印产品
        System.out.println("当前产品"+product);
        int result = agoniProductMapper.updateProduct(product);
        return result;
    }

    /**
     * 根据用户id查询商店id
     */
    @Override
    public Long getStoreIdByUserId(Long userId) {
        Long storeId = (long) agoniProductMapper.getStoreIdByUserId(userId);
        return storeId;
    }

    //用户添加产品


    @Override
    public int createProduct(Product product, Long userId) {
        int storeId = agoniProductMapper.getStoreIdByUserId(userId);
        product.setStoreId(storeId);
        int result = agoniProductMapper.addProduct(product);
        return result;
    }



    //获取他人商店产品
    @Override
    public List<OtherProductVO> listOtherProducts(Long userId) {
        Long storeId = (long) agoniProductMapper.getStoreIdByUserId(userId);
        List<Product> ProductList = agoniProductMapper.listOtherProducts(storeId);
        List<OtherProductVO> otherProductVOS = new ArrayList<>();

        for (Product product : ProductList) {
            OtherProductVO otherProductVO = OtherProductVO.builder()
                    .id(product.getId())
                    .name(product.getName())
                    .price(product.getPrice())
                    .description(product.getDescription())
                    .image(product.getImage())
                    .storeId(product.getStoreId())
                    .stock(product.getStock())
                    .status(product.getStatus())
                    .category(product.getCategory())
                    .salesVolume(product.getSalesVolume())
                    .discountPrice(product.getDiscountPrice())
                    .build();
            otherProductVOS.add(otherProductVO);
        }
        return otherProductVOS;
    }

    //分页获取他人商店产品

    @Override
    public PageInfo<OtherProductVO> listOtherProductsByPage(Long userId, ListOtherProductsByPageDTO listOtherProductsByPageDTO) {
        Long storeId = (long) agoniProductMapper.getStoreIdByUserId(userId);
        log.info("storeId:{}",storeId);
        log.info("listOtherProductsByPageDTO:{}",listOtherProductsByPageDTO);

        PageHelper.startPage(listOtherProductsByPageDTO.getPageNum(), listOtherProductsByPageDTO.getPageSize());
        List<Product> ProductList = agoniProductMapper.listOtherProductsByPage(storeId, listOtherProductsByPageDTO.getKeywords(),listOtherProductsByPageDTO.getProductTypeId());
        List<OtherProductVO> otherProductVOS = new ArrayList<>();

        for (Product product : ProductList) {
            OtherProductVO otherProductVO = OtherProductVO.builder()
                    .id(product.getId())
                    .name(product.getName())
                    .price(product.getPrice())
                    .description(product.getDescription())
                    .image(product.getImage())
                    .storeId(product.getStoreId())
                    .stock(product.getStock())
                    .status(product.getStatus())
                    .category(product.getCategory())
                    .salesVolume(product.getSalesVolume())
                    .discountPrice(product.getDiscountPrice())
                    .build();
            otherProductVOS.add(otherProductVO);
        }
        PageInfo<OtherProductVO> pageInfo = new PageInfo<>(otherProductVOS);
        return pageInfo;
    }


    //获取用我的商店产品信息

    @Override
    public List<MyProductVO> listMyProductVO(Long userId) {
        Long storeId = (long) agoniProductMapper.getStoreIdByUserId(userId);
        List<Product> ProductList = agoniProductMapper.listMyStoreProductVO(storeId);
        List<MyProductVO> myProductVOS = new ArrayList<>();
        for (Product product : ProductList) {
            MyProductVO myProductVO = MyProductVO.builder()
                    .id(product.getId())
                    .name(product.getName())
                    .price(product.getPrice())
                    .description(product.getDescription())
                    .image(product.getImage())
                    .storeId(product.getStoreId())
                    .stock(product.getStock())
                    .status(product.getStatus())
                    .category(product.getCategory())
                    .salesVolume(product.getSalesVolume())
                    .discountPrice(product.getDiscountPrice())
                    .build();
            myProductVOS.add(myProductVO);
        }


        return myProductVOS;
    }

    //用户上下架商品

    @Override
    public int changeProductStatus(Long id) {
        Product product = agoniProductMapper.selectProductById(id);
        if(product.getStatus() == 1){
            product.setStatus(0);
        }else{
            product.setStatus(1);
        }
        int result=0;
         result=agoniProductMapper.updateProduct(product);
        return result;
    }
}
